﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Aspose.Slides.Export;
using Aspose.Slides.Pptx;
using System.Drawing;
using System.Data;
using System.Globalization;

namespace PPTUtilities
{
    class PPTTable
    {
        static HyperlinkEx HLink = null;
        static KeyValuePair<string, int> screenPosValue = new KeyValuePair<string, int>();
        public static void AddTable(PresentationEx pres, int slideposition, int Xposition, int Yposition, int borderwidth, Color bordercol, Color headerBackCol, Color headerForeCol, Color RowForeCol, DataTable dt)
        {
            //Access first slide
            SlideEx sld = pres.Slides[slideposition];
            //Add table shape to slide
            double[] dblCols = new double[dt.Columns.Count];
            //int colWidth = (int)(560 / dt.Columns.Count);
            //for (int x = 0; x < dt.Columns.Count; x++)
            dblCols[0] = 350;
            dblCols[1] = 150;
            dblCols[2] = 150;
            int rowCount = dt.Rows.Count > 10 ? 12 : dt.Rows.Count + 2;
            double[] dblRows = new double[rowCount];
            for (int x = 0; x < rowCount; x++)
                dblRows[x] = 30;

            sld.Shapes.AddTable(Xposition, Yposition, dblCols, dblRows);
            TableEx tbl = sld.Shapes.OfType<TableEx>().Last();
            //Set border format for each cell
            foreach (RowEx row in tbl.Rows)
                foreach (CellEx cell in row)
                {
                    cell.BorderTop.FillFormat.FillType = FillTypeEx.Solid;
                    cell.BorderTop.FillFormat.SolidFillColor.Color = bordercol;
                    cell.BorderTop.Width = borderwidth;

                    cell.BorderBottom.FillFormat.FillType = FillTypeEx.Solid;
                    cell.BorderBottom.FillFormat.SolidFillColor.Color = bordercol;
                    cell.BorderBottom.Width = borderwidth;

                    cell.BorderLeft.FillFormat.FillType = FillTypeEx.Solid;
                    cell.BorderLeft.FillFormat.SolidFillColor.Color = bordercol;
                    cell.BorderLeft.Width = borderwidth;

                    cell.BorderRight.FillFormat.FillType = FillTypeEx.Solid;
                    cell.BorderRight.FillFormat.SolidFillColor.Color = bordercol;
                    cell.BorderRight.Width = borderwidth;
                    if (cell.FirstRowIndex > 0)
                    {
                        cell.TextFrame.Paragraphs[0].Portions[0].FillFormat.FillType = FillTypeEx.Solid;
                        cell.TextFrame.Paragraphs[0].Portions[0].FillFormat.SolidFillColor.Color = RowForeCol;
                    }
                    //if (cell.FirstRowIndex == 1 && cell.FirstColumnIndex == 0)
                    //{
                    //    HyperlinkEx HLink = new HyperlinkEx(pres.Slides[2]);
                    //    //HyperlinkEx HLink = new HyperlinkEx(new HyperlinkEx(pres.Slides[1]), "", tooltip, false, false, false);
                    //    cell.TextFrame.Paragraphs[0].Portions[0].HLinkClick = HLink;
                    //}
                    if (cell.FirstColumnIndex > 0)
                        cell.TextFrame.Paragraphs[0].ParagraphFormat.Alignment = TextAlignmentEx.Center;

                }
            foreach (CellEx cell in tbl.Rows[0])
            {
                // backcolor
                cell.FillFormat.FillType = FillTypeEx.Solid;
                cell.FillFormat.SolidFillColor.Color = headerBackCol;
            }
            foreach (CellEx cell in tbl.Rows[tbl.Rows.Count - 1])
            {
                // backcolor
                cell.FillFormat.FillType = FillTypeEx.Solid;
                cell.FillFormat.SolidFillColor.Color = headerBackCol;
                cell.TextFrame.Paragraphs[0].Portions[0].FillFormat.FillType = FillTypeEx.Solid;
                cell.TextFrame.Paragraphs[0].Portions[0].FillFormat.SolidFillColor.Color = headerForeCol;
                cell.TextFrame.Paragraphs[0].Portions[0].PortionFormat.FontBold = NullableBool.True;
            }
            //Merge cells 1 & 2 of row 1
            //tbl.MergeCells(tbl[0, 0], tbl[1, 0], false);
            int total = 0, total1 = 0, othersTotal = 0, othersTotal1 = 0;
            string tempColValue = string.Empty;
            //Add text to the merged cell
            for (int x = 0; x < dt.Columns.Count; x++)
                tbl[x, 0].TextFrame.Text = dt.Columns[x].ColumnName;
            for (int x = 1; x < dt.Rows.Count + 1; x++)
                for (int y = 0; y < dt.Columns.Count; y++)
                {
                    tempColValue = Convert.ToString(dt.Rows[x - 1][y]).Replace("\r", "").Replace("\n", "");
                    if (rowCount == 12 && x > 9)
                    {
                        if (y == 1)
                            othersTotal = othersTotal + Convert.ToInt32(tempColValue);
                        if (y == 2)
                            othersTotal1 = othersTotal1 + Convert.ToInt32(tempColValue);
                    }
                    else
                    {
                        if (y == 1)
                        {
                            total = total + Convert.ToInt32(tempColValue);
                            tempColValue = Convert.ToInt32(tempColValue).ToString("#,#", CultureInfo.InvariantCulture);
                        }
                        if (y == 2)
                        {
                            total1 = total1 + Convert.ToInt32(tempColValue);
                            tempColValue = Convert.ToInt32(tempColValue).ToString("#,#", CultureInfo.InvariantCulture);
                        }
                        tbl[y, x].TextFrame.Text = tempColValue.Length > 60 ? tempColValue.Substring(0, 60) + "..." : tempColValue;
                    }
                }
            if (rowCount == 12)
            {
                tbl[0, 10].TextFrame.Text = "Others";
                tbl[1, 10].TextFrame.Text = (othersTotal).ToString("#,#", CultureInfo.InvariantCulture);
                tbl[2, 10].TextFrame.Text = (othersTotal1).ToString("#,#", CultureInfo.InvariantCulture);
                tbl[0, 11].TextFrame.Text = "Total";
                tbl[1, 11].TextFrame.Text = (total + othersTotal).ToString("#,#", CultureInfo.InvariantCulture);
                tbl[2, 11].TextFrame.Text = (total1 + othersTotal1).ToString("#,#", CultureInfo.InvariantCulture);
            }
            else
            {
                tbl[0, dt.Rows.Count + 1].TextFrame.Text = "Total";
                tbl[1, dt.Rows.Count + 1].TextFrame.Text = (total + othersTotal).ToString("#,#", CultureInfo.InvariantCulture);
                tbl[2, dt.Rows.Count + 1].TextFrame.Text = (total1 + othersTotal1).ToString("#,#", CultureInfo.InvariantCulture);
            }
        }

        public static void AddTableWithLink(PresentationEx pres, int slideposition, int Xposition, int Yposition, int borderwidth, Color bordercol, Color headerBackCol, Color headerForeCol, Color RowForeCol, DataTable dt, Dictionary<string, int> screenPos)
        {
            //Access first slide
            SlideEx sld = pres.Slides[slideposition];
            //Add table shape to slide
            double[] dblCols = new double[dt.Columns.Count];
            int colWidth = (int)(560 / dt.Columns.Count);
            for (int x = 0; x < dt.Columns.Count; x++)
                dblCols[x] = colWidth;

            double[] dblRows = new double[dt.Rows.Count + 2];
            for (int x = 0; x < dt.Rows.Count + 2; x++)
                dblRows[x] = 30;

            sld.Shapes.AddTable(Xposition, Yposition, dblCols, dblRows);
            TableEx tbl = sld.Shapes.OfType<TableEx>().Last();
            //Set border format for each cell
            foreach (RowEx row in tbl.Rows)
                foreach (CellEx cell in row)
                {
                    cell.BorderTop.FillFormat.FillType = FillTypeEx.Solid;
                    cell.BorderTop.FillFormat.SolidFillColor.Color = bordercol;
                    cell.BorderTop.Width = borderwidth;

                    cell.BorderBottom.FillFormat.FillType = FillTypeEx.Solid;
                    cell.BorderBottom.FillFormat.SolidFillColor.Color = bordercol;
                    cell.BorderBottom.Width = borderwidth;

                    cell.BorderLeft.FillFormat.FillType = FillTypeEx.Solid;
                    cell.BorderLeft.FillFormat.SolidFillColor.Color = bordercol;
                    cell.BorderLeft.Width = borderwidth;

                    cell.BorderRight.FillFormat.FillType = FillTypeEx.Solid;
                    cell.BorderRight.FillFormat.SolidFillColor.Color = bordercol;
                    cell.BorderRight.Width = borderwidth;

                    if (cell.FirstRowIndex > 0)
                    {
                        cell.TextFrame.Paragraphs[0].Portions[0].FillFormat.FillType = FillTypeEx.Solid;
                        cell.TextFrame.Paragraphs[0].Portions[0].FillFormat.SolidFillColor.Color = RowForeCol;
                    }
                    //if (cell.FirstRowIndex == 1 && cell.FirstColumnIndex == 0)
                    //{
                    //    HyperlinkEx HLink = new HyperlinkEx(pres.Slides[2]);
                    //    //HyperlinkEx HLink = new HyperlinkEx(new HyperlinkEx(pres.Slides[1]), "", tooltip, false, false, false);
                    //    cell.TextFrame.Paragraphs[0].Portions[0].HLinkClick = HLink;
                    //}
                    if (cell.FirstColumnIndex > 0)
                        cell.TextFrame.Paragraphs[0].ParagraphFormat.Alignment = TextAlignmentEx.Center;


                }
            foreach (CellEx cell in tbl.Rows[0])
            {
                // backcolor
                cell.FillFormat.FillType = FillTypeEx.Solid;
                cell.FillFormat.SolidFillColor.Color = headerBackCol;
            }
            foreach (CellEx cell in tbl.Rows[tbl.Rows.Count - 1])
            {
                // backcolor
                cell.FillFormat.FillType = FillTypeEx.Solid;
                cell.FillFormat.SolidFillColor.Color = headerBackCol;
                cell.TextFrame.Paragraphs[0].Portions[0].FillFormat.FillType = FillTypeEx.Solid;
                cell.TextFrame.Paragraphs[0].Portions[0].FillFormat.SolidFillColor.Color = headerForeCol;
                cell.TextFrame.Paragraphs[0].Portions[0].PortionFormat.FontBold = NullableBool.True;
            }
            //Merge cells 1 & 2 of row 1
            //tbl.MergeCells(tbl[0, 0], tbl[1, 0], false);
            int total = 0, total1 = 0;
            string tempColValue = string.Empty;
            //Add text to the merged cell
            for (int x = 0; x < dt.Columns.Count; x++)
                tbl[x, 0].TextFrame.Text = dt.Columns[x].ColumnName;
            for (int x = 1; x < dt.Rows.Count + 1; x++)
                for (int y = 0; y < dt.Columns.Count; y++)
                {
                    tempColValue = Convert.ToString(dt.Rows[x - 1][y]).Replace("\r", "").Replace("\n", "");
                    if (y == 1)
                    {
                        total = total + Convert.ToInt32(tempColValue);
                        tempColValue = Convert.ToInt32(tempColValue).ToString("#,#", CultureInfo.InvariantCulture);
                    }
                    if (y == 2)
                    {
                        total1 = total1 + Convert.ToInt32(tempColValue);
                        tempColValue = Convert.ToInt32(tempColValue).ToString("#,#", CultureInfo.InvariantCulture);
                    }
                    tbl[y, x].TextFrame.Text = tempColValue;
                }
            tbl[0, dt.Rows.Count + 1].TextFrame.Text = "Total";
            tbl[1, dt.Rows.Count + 1].TextFrame.Text = total.ToString("#,#", CultureInfo.InvariantCulture);
            tbl[2, dt.Rows.Count + 1].TextFrame.Text = total1.ToString("#,#", CultureInfo.InvariantCulture);

            for (int y = 1; y < tbl.Rows.Count - 1; y++)
            {
                screenPosValue = screenPos.Where(x => x.Key == tbl[0, y].TextFrame.Text).FirstOrDefault();
                HLink = new HyperlinkEx(pres.Slides[Convert.ToInt16(screenPosValue.Value)]);
                //HyperlinkEx HLink = new HyperlinkEx(new HyperlinkEx(pres.Slides[1]), "", tooltip, false, false, false);
                tbl[0, y].TextFrame.Paragraphs[0].Portions[0].HLinkClick = HLink;
            }

        }


        public static void AddVersionTable(PresentationEx pres, int slideposition, int Xposition, int Yposition, int borderwidth, Color bordercol, Color headerBackCol, Color headerForeCol, Color RowForeCol, DataRow dr,string Mediatype)
        {
            //Access first slide
            SlideEx sld = pres.Slides[slideposition];
            //Add table shape to slide
            double[] dblCols = new double[2];
            dblCols[0] = 110;
            dblCols[1] = 200;
            double[] dblRows = new double[8];
            for (int x = 0; x < 8; x++)
                dblRows[x] = 30;

            sld.Shapes.AddTable(Xposition, Yposition, dblCols, dblRows);
            TableEx tbl = sld.Shapes.OfType<TableEx>().Last();
            //Set border format for each cell
            foreach (RowEx row in tbl.Rows)
                foreach (CellEx cell in row)
                {
                    cell.BorderTop.FillFormat.FillType = FillTypeEx.Solid;
                    cell.BorderTop.FillFormat.SolidFillColor.Color = bordercol;
                    cell.BorderTop.Width = borderwidth;

                    cell.BorderBottom.FillFormat.FillType = FillTypeEx.Solid;
                    cell.BorderBottom.FillFormat.SolidFillColor.Color = bordercol;
                    cell.BorderBottom.Width = borderwidth;

                    cell.BorderLeft.FillFormat.FillType = FillTypeEx.Solid;
                    cell.BorderLeft.FillFormat.SolidFillColor.Color = bordercol;
                    cell.BorderLeft.Width = borderwidth;

                    cell.BorderRight.FillFormat.FillType = FillTypeEx.Solid;
                    cell.BorderRight.FillFormat.SolidFillColor.Color = bordercol;
                    cell.BorderRight.Width = borderwidth;
                    if (cell.FirstRowIndex > 0)
                    {
                        cell.TextFrame.Paragraphs[0].Portions[0].FillFormat.FillType = FillTypeEx.Solid;
                        cell.TextFrame.Paragraphs[0].Portions[0].FillFormat.SolidFillColor.Color = RowForeCol;

                    }
                    if (cell.FirstColumnIndex > 0)
                    {
                        cell.TextFrame.Paragraphs[0].ParagraphFormat.Alignment = TextAlignmentEx.Center;
                        cell.TextFrame.Paragraphs[0].Portions[0].FillFormat.FillType = FillTypeEx.Solid;
                        cell.TextFrame.Paragraphs[0].Portions[0].FillFormat.SolidFillColor.Color = headerBackCol;
                    }

                }
            foreach (CellEx cell in tbl.Rows[0])
            {
                // backcolor
                cell.FillFormat.FillType = FillTypeEx.Solid;
                cell.FillFormat.SolidFillColor.Color = headerBackCol;
            }
            tbl.MergeCells(tbl[0, 0], tbl[1, 0], false);
            tbl[0, 0].TextFrame.Text = "DETALLES";
            DataColumn dc = null;
            string tempColValue = string.Empty;
            for (int x = 0; x < dblRows.Length - 1; x++)
            {
                dc = dr.Table.Columns[x];
                if (dc.ColumnName == "Dur/Tam")
                {
                    if (Mediatype == "TV")
                        tbl[0, x + 1].TextFrame.Text = "Duration";
                    else
                        tbl[0, x + 1].TextFrame.Text = "Size";
                }
                else
                    tbl[0, x + 1].TextFrame.Text = dc.ColumnName;
                tempColValue = Convert.ToString(dr[dc]).Replace("\r", "").Replace("\n", "");
                tbl[1, x + 1].TextFrame.Text = tempColValue.Length > 40 ? tempColValue.Substring(0, 40) + "..." : tempColValue;
            }
        }


        public static void RemoveTableBorders(string filename)
        {
            //Instantiate Presentation class that represents PPTX file
            using (PresentationEx pres = new PresentationEx())
            {

                //Access first slide
                SlideEx sld = pres.Slides[0];

                //Define columns with widths and rows with heights
                double[] dblCols = { 50, 50, 50 };
                double[] dblRows = { 50, 30, 30, 30, 30 };

                //Add table shape to slide
                sld.Shapes.AddTable(100, 50, dblCols, dblRows);
                TableEx tbl = sld.Shapes.OfType<TableEx>().First();
                //Set border format for each cell
                foreach (RowEx row in tbl.Rows)
                    foreach (CellEx cell in row)
                    {
                        cell.BorderTop.FillFormat.FillType = FillTypeEx.NoFill;
                        cell.BorderBottom.FillFormat.FillType = FillTypeEx.NoFill;
                        cell.BorderLeft.FillFormat.FillType = FillTypeEx.NoFill;
                        cell.BorderRight.FillFormat.FillType = FillTypeEx.NoFill;
                    }
                //Write PPTX to Disk
                pres.Save(filename, SaveFormat.Pptx);
            }
        }

    }
}
